package com.qf.zyh.services.Impl;

import com.qf.zyh.dao.CartDao;
import com.qf.zyh.dao.Impl.CartDaoImpl;
import com.qf.zyh.dao.Impl.ProductDaoImpl;
import com.qf.zyh.dao.ProductDao;
import com.qf.zyh.entity.Cart;
import com.qf.zyh.entity.Product;
import com.qf.zyh.services.CartSer;
import com.qf.zyh.util.DBUtil;
import org.apache.commons.dbutils.DbUtils;

import java.util.List;

public class CartSerImpl implements CartSer {
    CartDao cd = new CartDaoImpl();
    @Override
    public Boolean addCart(Integer pid, int uid) {
        try {
            DBUtil.begin();
            int i =0;
            //查询是否有数据。有则修改，无则增加
            Cart c = cd.selectcart(pid, uid);
            if (c!=null) {
                //修改数量与单价。
                c.setCnum(c.getCnum()+1);
                //设置新的小计为，pro的单价*新的数量。
                c.setCcount(c.getProduct().getPprice()*c.getCnum());
                 i = cd.updateCart(c.getCid(),c.getCnum(),c.getCcount());
            }else {
                //无则新增
                Cart cart = new Cart();
                ProductDao pd = new ProductDaoImpl();
                Product pro = pd.getid(pid);
                cart.setPid(pro.getPid());
                cart.setCnum(1);
                cart.setCcount(pro.getPprice());
                cart.setUid(uid);
                System.out.println(cart);
                i = cd.addCart(cart);
            }
            //在外部提交，全部操作结束后提交
            DBUtil.commit();
            System.out.println(i);
            if (i>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return false;
    }

    @Override
    public List<Cart> getAll(int uid) {
            return cd.getall(uid);
    }

    @Override
    public boolean daleCart(Integer pid, int uid) {
        //传入参数为0则清空，传入参数为对应id则删除单个
        try {
            DBUtil.begin();
            int i = 0;
            if (pid==0) {
                i = cd.daleCart(uid);
            }else {
                i = cd.deleCartid(uid,pid);
            }
            DBUtil.commit();
            if (i>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return false;
    }

    @Override
    public boolean updateCart(int uid, Integer pid, Integer num) {
        //如果修改后的个数为0则调用删除
        // 首先查询对应数据，然后获得修改后的数据，>0则修改
        try {
            DBUtil.begin();
            int i = 0;
            if (num==0) {
                i = cd.deleCartid(uid, pid);
            }else {
                Cart ct = cd.selectcart(pid, uid);
                ct.setCnum(ct.getCnum()+num);
                ct.setCcount(ct.getCnum()*ct.getProduct().getPprice());
                i = cd.updateCart(ct.getCid(), ct.getCnum(), ct.getCcount());
            }
            DBUtil.commit();
            if (i>0){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }

        return false;
    }
}
